package com.fingard.common.utils.json;

import com.fingard.text.StringHelper;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class JSONUtils {
	/**
	 * JSON字符串转Map
	 *
	 * @param json
	 * @return Map
	 */
	public static HashMap<String, String> json2Map(String json) {
		HashMap<String, String> retMap = new HashMap<String, String>();

		JSONObject tmpJson = JSONObject.fromObject(json);
		Iterator<?> it = tmpJson.keys();
		while (it.hasNext()) {
			String key = (String) it.next();
			retMap.put(key, tmpJson.getString(key));
		}

		return retMap;
	}

	/**
	 * Map转JSON字符串
	 *
	 * @param mapParams
	 * @return String
	 */
	public static String map2JSON(Map<String, String> mapParams) {
		JSONObject json = new JSONObject();

		Iterator<String> it = mapParams.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			json.put(key, mapParams.get(key));
		}

		return json.toString();
	}
}
